package com.sifang.sfuserservice.utils;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import com.fasterxml.jackson.annotation.JsonFormat;

import javax.annotation.PostConstruct;

public class SnowflakeUtil {

    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private static long workerId = 0;//为终端ID
    private static long dataCenterId = 1;//数据中心ID
    private static Snowflake snowflake = IdUtil.createSnowflake(workerId,dataCenterId);
    @PostConstruct
    public void init(){
        workerId = NetUtil.ipv4ToLong(NetUtil.getLocalhostStr());
    }
    public static synchronized String snowflakeStrId(){
        return String.valueOf(snowflake.nextId());
    }
    public static synchronized long snowflakeId(){
        return snowflake.nextId();
    }

}
